package com.p7.notepad.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.p7.notepad.R;
import com.p7.notepad.activity.AddNotepadActivity;
import com.p7.notepad.activity.LoginActivity;
import com.p7.notepad.activity.NotepadDetailsActivity;
import com.p7.notepad.activity.SearchActivity;
import com.p7.notepad.adapter.NotepadAdapter;
import com.p7.notepad.app.BaseFragment;
import com.p7.notepad.bean.Notepad;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;
import com.p7.notepad.tools.Storage;
import com.tools.s7.recycler.DividerItemDecoration;
import com.tools.s7.recycler.listener.Recycler;
import com.tools.s7.recycler.utils.LoadMoreHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewFragment extends BaseFragment implements LoadMoreHelper.LoadDataListener, Recycler.OnItemClickListener<Notepad.DataBean> {

    @BindView(R.id.new_srl)
    SwipeRefreshLayout mSrl;
    @BindView(R.id.new_data)
    RecyclerView mData;

    private LoadMoreHelper mHelper;

    public NewFragment() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_new;
    }

    @Override
    protected void initEvents() {
        super.initEvents();

        mHelper = new LoadMoreHelper.Builder<Notepad.DataBean>()
                .setPageSize(10)
                .setSwipeRefresh(mSrl)
                .setRecycler(mData, new LinearLayoutManager(getActivity()), new DividerItemDecoration(getActivity(), 1, 0xFFDFDFDF))
                .setAdapter(new NotepadAdapter(), false)
                .setLoadData(this)
                .setItemClick(this)
                .build();
    }

    @Override
    public void onResume() {
        super.onResume();
        mHelper.loadFirstPage();
    }

    @OnClick({R.id.new_search, R.id.new_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.new_search:
                startActivity(SearchActivity.class);
                break;
            case R.id.new_add:
                startActivity(AddNotepadActivity.class);
                break;
        }
    }

    @Override
    public void onLoadData(final int i) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("userId", String.valueOf(Storage.getId()));
        parameters.put("pageNum", String.valueOf(i));
        parameters.put("pageSize", String.valueOf(10));
        APIManager.onPost("notepad/getNotepadNew", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                Notepad notepad = gson.fromJson(s, Notepad.class);
                if (notepad.getCode() == 1000) {
                    mHelper.notifyAdapter(notepad.getData());
                } else if (notepad.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                } else {
                    if (i == 1) {
                        mHelper.notifyNull();
                    } else {
                        mHelper.notifyError();
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                mHelper.notifyError();
            }
        });
    }

    @Override
    public void onItemClick(int i, Notepad.DataBean dataBean) {
        Bundle bundle = new Bundle();
        bundle.putString("id", String.valueOf(dataBean.getId()));
        startActivity(NotepadDetailsActivity.class, bundle);
    }
}
